package com.fymxy.question_bank_management.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.ExamnateWithBLOBs;
import com.fymxy.question_bank_management.service.ExamnateService;

import com.fymxy.question_bank_management.pojo.*;
import com.fymxy.question_bank_management.service.ExamnateService;
import com.fymxy.question_bank_management.service.ProblemService;

import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ExamnateController {

    @Autowired
    ExamnateService examnateService;
    @Autowired
    ProblemService problemService;

    @PostMapping("student/autoCheck")
    public ApiResult autoCheck(@RequestBody ExamnateWithBLOBs examnateWithBLOBs) {

        //解析answerRecord转map数组
        String answerRecord = examnateWithBLOBs.getAnswerRecord();
        //键:problemNo,值:studentAnswer
//        Map<Integer,String> answerMap = new HashMap<>();
        String[] answerList = answerRecord.split("~");
        List<Integer> problemNos = new ArrayList<>();
        List<String> answers = new ArrayList<>();
        for (int i = 0; i < answerList.length; i++) {
            String[] record = answerList[i].split(":");
            Integer problemNo = Integer.parseInt(record[0]);
            if (record.length < 2) {
                problemNos.add(problemNo);
                answers.add("");
            } else {
                String studentAnswer = record[1];
                problemNos.add(problemNo);
                answers.add(studentAnswer);
            }
        }

        StringBuffer socreRecord = new StringBuffer();
        for (int i = 0; i < problemNos.size(); i++) {
            Integer problemNo = problemNos.get(i);
            ProblemWithBLOBs problem = problemService.selectAllByProblemNo(problemNo);
            String trueAnswer = problem.getProblemAnswerContent();
            String type = problem.getProblemType();
            int score = 0;
            switch (type) {
                case "choice":
                    if (trueAnswer.equals(answers.get(i))) {
                        score = 3;
                    }

                case "fill":
                    if (trueAnswer.equals(answers.get(i))) {
                        score = 4;
                    }

                case "cal":
                    if (trueAnswer.equals(answers.get(i))) {
                        score = 10;
                    }
            }
            socreRecord.append(problemNo + ":" + score + "~");
        }
        //插入考试记录
        examnateWithBLOBs.setScoreRecord(socreRecord.deleteCharAt(socreRecord.length() - 1).toString());
//        examnateWithBLOBs.setCheckStatus("done");
        examnateService.insert(examnateWithBLOBs);

        return ApiResultHandler.buildApiResult(200, "请求成功", socreRecord);
    }


    @PostMapping("/changeExamnate")
    public ApiResult changeExamnate(@RequestBody ExamnateWithBLOBs examnateWithBLOBs) {
        return ApiResultHandler.buildApiResult(200, "查询做题记录成功", examnateService.update(examnateWithBLOBs));
    }

    @PostMapping("/addExamnate")
    public ApiResult addExamnate(@RequestBody ExamnateWithBLOBs examnateWithBLOBs) {
        return ApiResultHandler.buildApiResult(200, "查询做题记录成功", examnateService.insert(examnateWithBLOBs));
    }

    @GetMapping("/viewAnswer/{studentNo}/{examCode}")
    public ApiResult viewAnswer(@PathVariable("studentNo") Integer studentNo, @PathVariable("examCode") Integer paperNo) {

        ExamnateWithBLOBs studentAnswer = examnateService.selectAnswerByStudentNoAndPaperNo(studentNo, paperNo);
        return ApiResultHandler.buildApiResult(200, "查看学生答案成功", studentAnswer);

    }

    @GetMapping("/showTeacherExamnates/{page}/{teacherNo}")
    public ApiResult showTeacherExamnater(@PathVariable("page") Integer page,@PathVariable("teacherNo") Integer teacherNo){
        Page<StudentExamnate> studentExamnatePage = new Page<>(page,10);
        IPage<StudentExamnate> studentExamnateIPage = examnateService.selectExamnateByTeacher(studentExamnatePage,teacherNo);
        return ApiResultHandler.buildApiResult(200,"查询做题记录成功",studentExamnateIPage);
    }

    @GetMapping("/showStudentExamnates/{page}/{studentNo}")
    public ApiResult showStudentExamnater(@PathVariable("page") Integer page,@PathVariable("studentNo") Integer studentNo){
        Page<StudentExamnate> studentExamnatePage = new Page<>(page,10);
        IPage<StudentExamnate> studentExamnateIPage = examnateService.selectExamnateByStudent(studentExamnatePage,studentNo);
        return ApiResultHandler.buildApiResult(200,"查询做题记录成功",studentExamnateIPage);
    }
    @GetMapping("/showExamnates/{page}")
    public ApiResult showExamnates(@PathVariable("page") Integer page){
        Page<StudentExamnate> studentExamnatePage = new Page<>(page,10);
        IPage<StudentExamnate> studentExamnateIPage = examnateService.selectAllExamnate(studentExamnatePage);
        return ApiResultHandler.buildApiResult(200,"查询做题记录成功",studentExamnateIPage);
    }
}